package controller

import (
	"net/http"
	"server_cluster/http/model"
	"server_cluster/http/pkg/httpclient"

	"github.com/gin-gonic/gin"
	"server_cluster/common/logger"
)

// 获取停服信息
func (c *Controller) gmServerConfigStopServerGet(ctx *gin.Context) {
	stopServer, err := c.s.GetStopServer()
	if err != nil {
		logger.Error("get stop server error: %v", err)
		ctx.JSON(http.StatusOK, UnknownError)
		return
	}
	ctx.JSON(http.StatusOK, &model.CommonRsp{Code: 0, Data: &model.GmServerConfigStopServerGetRsp{StopServer: stopServer}})
}

// 修改停服信息
func (c *Controller) gmServerConfigStopServerSet(ctx *gin.Context) {
	req := new(model.GmServerConfigStopServerSetReq)
	err := ctx.ShouldBindJSON(req)
	if err != nil {
		logger.Error("parse json error: %v", err)
		ctx.JSON(http.StatusOK, ParamError)
		return
	}
	err = c.s.SetStopServer(req.StopServer)
	if err != nil {
		logger.Error("set stop server error: %v", err)
		ctx.JSON(http.StatusOK, UnknownError)
		return
	}
	ctx.JSON(http.StatusOK, Ok)
}

// 获取ip白名单
func (c *Controller) gmServerConfigIpWhiteGet(ctx *gin.Context) {
	ipWhiteList, err := c.s.GetIpWhite()
	if err != nil {
		logger.Error("get ip white error: %v", err)
		ctx.JSON(http.StatusOK, UnknownError)
		return
	}
	ctx.JSON(http.StatusOK, &model.CommonRsp{Code: 0, Data: &model.GmServerConfigIpWhiteGetRsp{IpWhiteList: ipWhiteList}})
}

// 添加ip白名单
func (c *Controller) gmServerConfigIpWhiteAdd(ctx *gin.Context) {
	req := new(model.GmServerConfigIpWhiteAddReq)
	err := ctx.ShouldBindJSON(req)
	if err != nil {
		logger.Error("parse json error: %v", err)
		ctx.JSON(http.StatusOK, ParamError)
		return
	}
	if req.IpWhite == "" {
		ctx.JSON(http.StatusOK, ParamError)
		return
	}
	err = c.s.IpWhiteAdd(req.IpWhite)
	if err != nil {
		logger.Error("ip white add error: %v", err)
		ctx.JSON(http.StatusOK, UnknownError)
		return
	}
	ctx.JSON(http.StatusOK, Ok)
}

// 删除ip白名单
func (c *Controller) gmServerConfigIpWhiteDel(ctx *gin.Context) {
	req := new(model.GmServerConfigIpWhiteDelReq)
	err := ctx.ShouldBindJSON(req)
	if err != nil {
		logger.Error("parse json error: %v", err)
		ctx.JSON(http.StatusOK, ParamError)
		return
	}
	if req.IpWhite == "" {
		ctx.JSON(http.StatusOK, ParamError)
		return
	}
	err = c.s.IpWhiteDel(req.IpWhite)
	if err != nil {
		logger.Error("ip white del error: %v", err)
		ctx.JSON(http.StatusOK, UnknownError)
		return
	}
	ctx.JSON(http.StatusOK, Ok)
}

// 获取uid白名单
func (c *Controller) gmServerConfigUidWhiteGet(ctx *gin.Context) {
	uidWhiteList, err := c.s.GetUidWhite()
	if err != nil {
		logger.Error("get uid white error: %v", err)
		ctx.JSON(http.StatusOK, UnknownError)
		return
	}
	tmp := make([]int32, 0, len(uidWhiteList))
	for _, v := range uidWhiteList {
		tmp = append(tmp, int32(v))
	}
	ctx.JSON(http.StatusOK, &model.CommonRsp{Code: 0, Data: &model.GmServerConfigUidWhiteGetRsp{UidWhiteList: tmp}})
}

// 添加uid白名单
func (c *Controller) gmServerConfigUidWhiteAdd(ctx *gin.Context) {
	req := new(model.GmServerConfigUidWhiteAddReq)
	err := ctx.ShouldBindJSON(req)
	if err != nil {
		logger.Error("parse json error: %v", err)
		ctx.JSON(http.StatusOK, ParamError)
		return
	}
	if req.UidWhite == 0 {
		ctx.JSON(http.StatusOK, ParamError)
		return
	}
	err = c.s.UidWhiteAdd(uint32(req.UidWhite))
	if err != nil {
		logger.Error("uid white add error: %v", err)
		ctx.JSON(http.StatusOK, UnknownError)
		return
	}
	ctx.JSON(http.StatusOK, Ok)
}

// 删除uid白名单
func (c *Controller) gmServerConfigUidWhiteDel(ctx *gin.Context) {
	req := new(model.GmServerConfigUidWhiteDelReq)
	err := ctx.ShouldBindJSON(req)
	if err != nil {
		logger.Error("parse json error: %v", err)
		ctx.JSON(http.StatusOK, ParamError)
		return
	}
	if req.UidWhite == 0 {
		ctx.JSON(http.StatusOK, ParamError)
		return
	}
	err = c.s.UidWhiteDel(uint32(req.UidWhite))
	if err != nil {
		logger.Error("uid white del error: %v", err)
		ctx.JSON(http.StatusOK, UnknownError)
		return
	}
	ctx.JSON(http.StatusOK, Ok)
}

// 执行gm命令
func (c *Controller) gmServerCmd(ctx *gin.Context) {
	req := new(model.GmServerCmdReq)
	err := ctx.ShouldBindJSON(req)
	if err != nil {
		logger.Error("parse json error: %v", err)
		ctx.JSON(http.StatusOK, ParamError)
		return
	}
	rsp, err := httpclient.PostJson[model.GmServerCmdRsp]("http://"+req.ServerAddr+"/gm/cmd", req)
	if err != nil {
		logger.Error("post json error: %v", err)
		ctx.JSON(http.StatusOK, UnknownError)
		return
	}
	ctx.JSON(http.StatusOK, &model.CommonRsp{Code: 0, Data: rsp})
}
